<?php
namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller
{
	public function index()
	{
		$this->display();
	}
	public function login()
	{	
		//	接收用户名和密码
		$name=I('post.name');
		$password=I('post.password');

		// 验证用户	
		$data=M('user')->where(array('name'=>$name))->find();
		// echo $name;echo $password;var_dump($data);exit;
		if(!$data){
			$this->error('用户名不存在！');
			exit;
		}
		if($data['password'] != $password){
			$this->error('密码不正确！');
			exit;
		}

		// 把用户信息添加到sission中
		$_SESSION['admin_user']=$data;
		//select * from jsp_jq,jsp_quanxian where (select jsp_jq.qid from jsp_uj,jsp_jq where (select jsp_uj.jid from jsp_user,jsp_uj where jsp_user.id=jsp_uj.uid)=jsp_jq.jid)=jsp_quanxian.id;

		$list=M('quanxian')->field('controller,method')->where('id in'.M('jq')->field('qid')->where('jid in'.M('uj')->field('jid')->where(array('uid'=>array('eq',$data['id'])))->buildSql())->buildSql())->select();
		// echo "<pre>";
		// var_dump($list);
		// echo '</pre>';exit;
		foreach ($list as $key => $val) {
			$list[$key]['controller'] = ucfirst($val['controller']);
		}
		$nodelist = array();
		$nodelist['Index'] = array('index');
		//遍历重新拼装
		foreach($list as $v){
			$nodelist[$v['controller']][] = $v['method'];
			//把修改和执行修改 添加和执行添加 拼装到一起
			if($v['method']=="edit"){
				$nodelist[$v['controller']][]="save";
			}
			if($v['method']=="add"){
				$nodelist[$v['controller']][]="doadd";
			}
		}

		//将权限信息放置到session中
		$_SESSION['admin_user']['nodelist'] = $nodelist;

		// 重组的信息
		// echo "<pre>";
		// var_dump($_SESSION);
		// echo '</pre>';exit;
		if(empty($nodelist[$controller]) || !in_array($method,$nodelist[$controller])){
				
				$this->error("抱歉！没有操作权限！");
				exit;
			}

		//跳转到首页
		$this->redirect('index/index');
		
	}
	//退出登陆
	public function logout()
	{
		//清空session
		unset($_SESSION['admin_user']);
		//跳转
		$this->redirect('Public/index');
	}
}